package edu.gyc.myshop.controller;


import edu.gyc.myshop.model.Goods;
import edu.gyc.myshop.model.Goodstype;
import edu.gyc.myshop.service.GoodsService;
import edu.gyc.myshop.service.GoodstypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2024-02-05
 */
@Controller

public class GoodsController {


    @Autowired
    GoodsService goodsService;

    @RequestMapping("/type")
    public String myGoods(String tid,Model model) {
        List<Goods> goodsList=goodsService.lambdaQuery()
                          .eq(Goods::getGoodstypeId,tid)
                            .list();
        model.addAttribute("goodsList", goodsList);
        return "/list";
    }

    @RequestMapping("/search")
    public String search(String name,Model model) {

        List<Goods> goodsList=goodsService.lambdaQuery()
                .like(Goods::getName,name)
                .list();

        model.addAttribute("goodsList", goodsList);
        return "/list";
    }

    @Autowired
    GoodstypeService goodstypeService;

    @RequestMapping("/goodsDetail")
    public String goods(String id,String msg,Model model) {


      Goods goods=goodsService.lambdaQuery()
                .eq(Goods::getId,id)
                .one();

        Goodstype goodstype = goodstypeService.lambdaQuery()
                .eq(Goodstype::getId, goods.getGoodstypeId())
                .one();
        goods.setTypename(goodstype.getTypename());

        model.addAttribute("goods", goods);
        if (msg != null) {
            model.addAttribute("msg", msg);
        }
        return "/goods";
    }
}
